package com.jiuyun.dao.impi;

import com.jiuyun.dao.QuestionDao;
import com.jiuyun.entity.Question;
import com.jiuyun.entity.Question1;
import com.jiuyun.util.AliPool;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class QuestionDaoImpi  implements QuestionDao {
    QueryRunner runner=new QueryRunner();
    Connection conn;
    PreparedStatement pst;
    ResultSet rs;

    @Override
    public List<Question1> find(String code_name) throws SQLException {
        String sql="select q.*,s.question_fraction from exam as e INNER JOIN  `code` as c on e.exam_code=c.code_name INNER JOIN subjec as s \n" +
                "\t\ton c.code_id=s.code_id INNER JOIN question as q on s.question_id=q.question_id\n" +
                "\t\t\twhere c.code_name=?";
        conn= AliPool.getConn();
        List<Question1> list=runner.query(conn,sql,new BeanListHandler<Question1>(Question1.class),code_name);
        conn.close();
        return list;
    }

    @Override
    public List<Question1> tmfind(String code_name,int ym) throws SQLException {
        String sql="select q.*,s.question_fraction from exam as e INNER JOIN  `code` as c on e.exam_code=c.code_name INNER JOIN subjec as s " +
                "on c.code_id=s.code_id INNER JOIN question as q on s.question_id=q.question_id " +
                " where c.code_name=? LIMIT ?,1";
        conn= AliPool.getConn();
        List<Question1> list=runner.query(conn,sql,new BeanListHandler<Question1>(Question1.class),code_name,ym);
        conn.close();
        return list;
    }

    @Override
    public Question1 js(String daan, int question_id, String exam_name) throws SQLException {
        String sql="select q.*,s.question_fraction from exam as e INNER JOIN  `code` as c on e.exam_code=c.code_name INNER JOIN subjec as s  " +
                " on c.code_id=s.code_id INNER JOIN question as q on s.question_id=q.question_id " +
                " where q.question_id=? and q.question_answer=? and e.exam_name=?";
        conn=AliPool.getConn();
        Question1 question=runner.query(conn,sql,new BeanHandler<Question1>(Question1.class),question_id,daan,exam_name);
        conn.close();
        return question;
    }



    public static void main(String[] args) throws SQLException {
        QuestionDao dao=new QuestionDaoImpi();
        System.out.println(dao.js("D",1,"234班HTML基础"));
    }

}
